#!/usr/bin/python

import sys
import re

def getvalue(a):
   return a[a.find("=")+1:].strip("\"")

def mean(L):
   return sum(L) / len(L)

if len(sys.argv) <= 1:
   print "file name?"
   sys.exit(-1)

tasks = {}
job   = {}
task_types = set()
for l in open(sys.argv[1]):
   A = l.split()
   if A[0] == "Job":
       for a in A:
           if a.startswith("LAUNCH_TIME"):
               job["launch_time"] = int(getvalue(a))
           if a.startswith("FINISH_TIME"):
               job["finish_time"] = int(getvalue(a))

   elif A[0] == "Task":
       task = {}
       for a in A:
           if a.startswith("TASKID"):
               task["taskid"] = getvalue(a)
           if a.startswith("START_TIME"):
               task["start_time"] = int(getvalue(a))
           if a.startswith("FINISH_TIME"):
               task["finish_time"] = int(getvalue(a))
           if a.startswith("SPLITS"):
               task["tracker"] = getvalue(a)

           if a.startswith("TASK_TYPE"):
               task["task_type"] = getvalue(a)
       try:
           if task["tracker"] == "":
               print repr(task["task_type"]), type(task["task_type"])
               print l
       except KeyError:
           pass
       if task["task_type"] in ["SETUP", "CLEANUP"]:
           continue
       if not tasks.has_key(task["taskid"]):
           tasks[task["taskid"]] = {}
       for k,v in task.items():
           tasks[task["taskid"]][k] = v


job["life"] = job["finish_time"] - job["launch_time"]
cmptlist = []
trackers = {}
taskslist = tasks.values()
for task in taskslist:
   t = task["finish_time"] - task["start_time"]
   tracker = task["tracker"]
   task["life"] = t
   cmptlist.append(t)
   if  not  trackers.has_key(tracker):
       trackers[tracker] = [t]
   else:
       trackers[tracker].append(t)

print "job completion time:", job["life"]
print "task completion time: mean %.2f, min %.2f, max %.2f"%(mean(cmptlist), min(cmptlist), max(cmptlist))
print "per tracker"
for tracker, tl in trackers.items():
   print "%s: mean  %.2f,  max %.2f, sum %.2f, len %d"%(repr(tracker), mean(tl), max(tl), sum(tl), len(tl))

